package demoMod.stsconsole.powers;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.core.AbstractCreature;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.enums.SubscriberPriority;
import demoMod.stsconsole.helpers.LocalizedStrings;
import demoMod.stsconsole.interfaces.ISubscriber;
import demoMod.stsconsole.interfaces.ReloadStringsSubscriber;
import demoMod.stsconsole.rooms.AbstractRoom;

import java.util.List;

public abstract class AbstractPower implements ReloadStringsSubscriber {
    public String id;
    public String name;
    public String description;
    protected List<String> powerStrings;
    protected List<String> commonStrings;
    public AbstractCreature owner;
    public PowerType type = PowerType.BUFF;
    public int[] amount;
    public boolean isDone = false;

    public AbstractPower(String id, AbstractCreature owner, int amount) {
        this(id, owner, new int[]{amount});
    }

    public AbstractPower(String id, AbstractCreature owner, int[] amount) {
        this.id = id;
        onReloadStrings();
        this.owner = owner;
        this.amount = amount;
        ConsoleEntry.addLater.add(this);
    }

    public abstract String getUpdatedDescriptions();

    public void onRemove() {
        this.isDone = true;
        if (this.owner != null && !this.owner.isDeadOrEscaped() && AbstractDungeon.currDungeon.currMapNode.room.phase != AbstractRoom.RoomPhase.COMPLETED) {
            this.owner.talk(this.name + commonStrings.get(0));
        }
        if (!ConsoleEntry.removeLater.contains(this)) ConsoleEntry.removeLater.add(this);
    }

    public void stackAmount(int[] amount) {
        int i = 0;
        for (int j : amount) {
            if (i >= this.amount.length) break;
            this.amount[i] += j;
            i++;
        }
    }

    public void onInitialApplication() {

    }

    @Override
    public void onReloadStrings() {
        powerStrings = LocalizedStrings.getPowerStrings(id);
        commonStrings = LocalizedStrings.getPowerStrings("AbstractPower");
        this.name = powerStrings.get(0);
        this.description = powerStrings.get(1);
    }

    @Override
    public int getPriority() {
        return SubscriberPriority.POWER.getPriority();
    }

    @Override
    public boolean enabled(Class<? extends ISubscriber> cls) {
        return this.owner != null && !this.owner.isDead && !this.isDone;
    }

    public enum PowerType {
        BUFF, DEBUFF
    }
}
